const express=require("express");
const router=express.Router();

const db=require("../db/db.js");

router.get("/selectAll",(req,res)=>{
	let sql="select * from user";
	db.query(sql,(err,data)=>{
		if(err){
			res.send({msg:err.message});
			return;
		}else{
			res.send({
				msg:"success",
				data:data,
				code:200,
			})
		}
	})
})

router.post("/insertUser",(req,res)=>{
	let sql="insert into user(username,password,tel,address,sex,age,identity) values(?,?,?,?,?,?,?)";
	let obj=[req.body.username,req.body.password,req.body.tel,req.body.address,req.body.sex,req.body.age,req.body.identity]
	db.query(sql,obj,(err,data)=>{
		if(err){
			res.send({
				msg:err.message
			})
			return;
		}else{
			res.send({
				code:200,
				msg:"添加成功"
			})
		}
	})
})

router.delete("/deleteUser",(req,res)=>{
	let id=req.body.id
	let sql='delete from user where id=?'
	console.log(id)
	db.query(sql,id,(err,data)=>{
		if(err){
			res.send({
				msg:err.message
			})
			return;
		}else{
			res.send({
				code:200,
				msg:"删除成功"
			})
		}
	})
})
router.post("/updateUser",(req,res)=>{
	let sql="update user set username=?,password=?,tel=?,address=?,sex=?,age=?,identity=? where id=?";
	let sj=[req.body.username,req.body.password,req.body.tel,req.body.address,req.body.sex,req.body.age,req.body.identity,req.body.id]
	db.query(sql,sj,(err,data)=>{
		if(err){
			res.send({
				msg:err.message
			})
			return;
		}else{
			res.send({
				code:200,
				msg:"修改成功",
			})
		}
	})
})

module.exports=router